package com.xiaoshuidi.cloud.module.member.xxljob;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.member.enums.RectificationItemStatusEnum;
import com.xiaoshuidi.cloud.module.member.enums.SecurityCheckOverdueStatusEnum;
import com.xiaoshuidi.cloud.module.member.mapper.securitycheck.SecurityCheckRectificationItemMapper;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheck.SecurityCheckRectificationItemDO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class SecurityCheckItemJob {

    private final SecurityCheckRectificationItemMapper securityCheckRectificationItemMapper;

    /**
     * 逾期检查
     */
    @XxlJob("overdue-check")
    @TenantIgnore
    @Transactional(rollbackFor = Exception.class)
    public void overdueCheck(){
        log.info("**********逾期检查任务开始执行**********");
        // 获取状态为待处理且超过整治日期的整改项
        QueryWrapper<SecurityCheckRectificationItemDO> wrapper = Wrappers.query();
        wrapper.eq("mscri.status", RectificationItemStatusEnum.PENDING.getCode());
        wrapper.lt("msc.timeframe", LocalDate.now());
        List<SecurityCheckRectificationItemDO> securityCheckRectificationItemDOS = securityCheckRectificationItemMapper.list(wrapper);
        log.info("逾期未完成的任务:{}", JsonUtils.toJsonString(securityCheckRectificationItemDOS));
        if(ObjectUtil.isNotEmpty(securityCheckRectificationItemDOS)){
            securityCheckRectificationItemDOS.stream().forEach(p -> p.setIsOverdue(SecurityCheckOverdueStatusEnum.OVERDUE.getValue()));
            securityCheckRectificationItemMapper.updateBatch(securityCheckRectificationItemDOS, securityCheckRectificationItemDOS.size());
        }

        // 即将逾期的任务
        wrapper = Wrappers.query();
        wrapper.eq("mscri.status", RectificationItemStatusEnum.PENDING.getCode());
        wrapper.in("msc.timeframe",
                        CollectionUtil.toList(LocalDate.now(),
                                LocalDate.now().plusDays(1),
                                LocalDate.now().plusDays(2))
                );
        List<SecurityCheckRectificationItemDO> overdueSoonDOS = securityCheckRectificationItemMapper.list(wrapper);
        log.info("即将逾期的任务:{}", JsonUtils.toJsonString(overdueSoonDOS));
        if(ObjectUtil.isNotEmpty(overdueSoonDOS)){
            overdueSoonDOS.stream().forEach(p -> p.setIsOverdue(SecurityCheckOverdueStatusEnum.OVERDUE_SOON.getValue()));
            securityCheckRectificationItemMapper.updateBatch(overdueSoonDOS, overdueSoonDOS.size());
        }
        // 逾期后完成的,状态更新为已完成
        wrapper = Wrappers.query();
        wrapper.eq("mscri.status", RectificationItemStatusEnum.COMPLETED.getCode());
        wrapper.in("mscri.is_overdue", CollectionUtil.toList(SecurityCheckOverdueStatusEnum.OVERDUE.getValue(), SecurityCheckOverdueStatusEnum.OVERDUE_SOON.getValue()));
        List<SecurityCheckRectificationItemDO> overdueDOS = securityCheckRectificationItemMapper.list(wrapper);
        log.info("逾期但已完成的任务:{}", JsonUtils.toJsonString(overdueDOS));
        if(ObjectUtil.isNotEmpty(overdueDOS)){
            overdueDOS.stream().forEach(p -> p.setIsOverdue(SecurityCheckOverdueStatusEnum.NOT_OVERDUE.getValue()));
            securityCheckRectificationItemMapper.updateBatch(overdueDOS, overdueDOS.size());
        }

        log.info("**********逾期检查任务执行完成**********");
    }

}
